//
//  Slide1VC.m
//  AMSlideMenu
//
//  Created by Artur Mkrtchyan on 1/27/14.
//  Copyright (c) 2014 SocialObjects Software. All rights reserved.
//

#import "Slide1VC.h"
#import "UIViewController+AMSlideMenu.h"
#import "firstTableViewController.h"
#import "secondTableViewController.h"
#import "QRViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "settingVc.h"
#import "SvGifView.h"


@interface Slide1VC ()// <UITableViewDataSource, UIAccelerometerDelegate>
{
    firstTableViewController * vc1;
    secondTableViewController * vc2;
    
    SvGifView* gifView;
    
    int flag;
   
}
@property (weak, nonatomic) IBOutlet UIButton *bottombtn;


@end

@implementation Slide1VC

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"viewDidLoad~~~问题出在 这个控制器总会被重新的创建");
    vc1=[[firstTableViewController alloc] init];
    vc1.tableView.frame = self.contentView.bounds;
    [self.contentView addSubview: vc1.tableView];
    
    vc2=[[secondTableViewController alloc] init];
    vc2.tableView.frame = self.contentView.bounds;
    [self.contentView addSubview: vc2.tableView];
    vc2.tableView.hidden = YES;
    
    [self.bottombtn bringSubviewToFront:self.view];
    
//    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
//
//    [center addObserverForName:@"setting" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
//        NSNumber * num = note.userInfo[@"indexPath"];
//        if([num isEqual:@0]) {
//            NSLog(@"回调闭包~~~");
//           // [self.navigationController pushViewController:[[settingVc alloc] init] animated:YES];
//        }
//    }];
    //////////////////
    NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"niao" withExtension:@"gif"];
    gifView = [[SvGifView alloc] initWithCenter:CGPointMake(self.view.bounds.size.width / 2, 80) fileURL:fileUrl];
    gifView.backgroundColor = [UIColor yellowColor];
    gifView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    
    UIApplication* app=[[UIApplication sharedApplication]init];
    [app.windows.firstObject addSubview:gifView];

    flag=1;

}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
   
}
#pragma mark - UIbutton
- (IBAction)bottomClick:(UIButton *)sender {
    

    if(0==flag){
        
        [gifView removeFromSuperview];
//        gifView.center = CGPointMake(self.view.bounds.size.width / 2, 80);
        
      flag=1;
        
    }else{

        [gifView startGif];
        [Slide1VC gifViewChange:gifView withNum:0];   //0:改变它的frame的x,y的值; 1:设置翻转方向
        flag=0;
//        if(gifView.center.y>self.view.frame.size.height){
//           //[gifView.layer removeAllAnimations];
//            gifView.center = CGPointMake(self.view.bounds.size.width / 2, 80);
//        }
    }
}


- (IBAction)addClick:(UIBarButtonItem *)sender {
    if ([self validateCamera] && [self canUseCamera]) {
        
        [self showQRViewController];
        
    } else {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"没有摄像头或摄像头不可用" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }
}


#pragma mark - UISegmentedControl


- (IBAction)segmentvaleChange:(UISegmentedControl *)sender {
    
    if(sender.selectedSegmentIndex == 0) {
        vc1.tableView.hidden = NO;
        vc2.tableView.hidden = YES;
        
    }else{
        vc1.tableView.hidden = YES;
        vc2.tableView.hidden = NO;
    }
}
#pragma mark - saoma
-(BOOL)canUseCamera {
    
    NSString *mediaType = AVMediaTypeVideo;
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
    if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
        
        NSLog(@"相机权限受限");
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请在设备的设置-隐私-相机中允许访问相机。" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        return NO;
    }
    return YES;
}

- (void)showQRViewController {
    
    QRViewController *qrVC = [[QRViewController alloc] init];
    [self.navigationController pushViewController:qrVC animated:YES];
}
-(BOOL)validateCamera {
    
    return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] &&
    [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
}

+(void) gifViewChange:(SvGifView*) viewGif withNum:(int)num{
    
    CGAffineTransform  transformR,transformS,transformI;
    //设置旋转度数
    transformR = CGAffineTransformRotate(viewGif.transform,M_PI/2.0);
    //缩放动画规模
    transformS = CGAffineTransformScale(viewGif.transform,0.8,0.8);
    transformI = CGAffineTransformInvert(viewGif.transform);
    
    //开始动画
    [UIView beginAnimations:@"move" context:nil];
    //设置时常
    [UIView setAnimationDuration:3];
    //设置代理
    [UIView setAnimationDelegate:self];
    
    //改变它的frame的x,y的值
    viewGif.center = CGPointMake(viewGif.center.x, viewGif.center.y+500);
    
    //动画结束
    [UIView commitAnimations];

}

@end
